<?php
date_default_timezone_set('Asia/Bangkok');
include get_template_directory() . '/widgets/blocknewslatest.php';
include get_template_directory() . '/widgets/blocknewscategory.php';
global $titles;

function getItemPostNews() {
    ob_start();
    $img = getURLImage(get_the_ID());
    $targetlink = get_post_meta(get_the_ID(), 'post_link_target', TRUE);
    $timeString = get_the_date();
    $timeUnix = get_post_time();
    $domain = getURLExtend(get_the_ID());
    if ($img) {
        ?>
        <div class="row">
            <div class="col-xs-4 col-sm-2 col-md-4 tc p0">
                <img class="img-responsive img-thumbnail mc" src="<?php echo $img; ?>" />
                <div class="time"><?php echo $domain; ?></div>
                <div class="time converttimetostring mb10" data-timeUnix="<?php echo $timeUnix; ?>" data-string="<?php echo $timeString; ?>"></div>
            </div>
            <div class="col-xs-8 col-sm-10 col-md-8">
                <h1><a target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo $targetlink; ?>" onclick="javascript:_gaq.push(['_trackEvent', 'extend-link', '<?php echo $targetlink; ?>']);"><?php the_title(); ?></a></h1>
                <div class="mtb10">
                    <a class="fl" target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo get_permalink(get_the_ID()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'single-detail', '<?php echo get_permalink(get_the_ID()); ?>']);">
                        <span class="glyphicon glyphicon-comment"></span>
                        <span class="fb-comment-count" data-href="<?php echo get_permalink(get_the_ID()); ?>"></span>
                    </a>
                    <span class="ml10 fbshare fbshare_0" data-oglink="<?php echo $targetlink; ?>" data-caption="<?php bloginfo('url'); ?>" data-link="<?php echo get_permalink(); ?>" data-picture="<?php echo esc_attr($img); ?>" data-description="<?php echo esc_attr(get_the_excerpt()); ?>" data-name="<?php echo esc_attr(get_the_title()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'facebook-share', '<?php echo get_permalink(get_the_ID()); ?>']);"></span>
                    <span class="fbshare_1"></span>
                    <span class="fbshare_2"></span>
                    <div class="clearfix"></div>
                </div>
                <div class="description-news"><?php the_excerpt(); ?></div>
            </div>
        </div>
        <?php
    } else {
        ?>
        <div class="row">
            <h1><a target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo $targetlink; ?>" onclick="javascript:_gaq.push(['_trackEvent', 'extend-link', '<?php echo $targetlink; ?>']);"><?php the_title(); ?></a></h1>
            <span class="time">dantri.com</span>
            <span class="time converttimetostring" data-timeUnix="<?php echo $timeUnix; ?>" data-string="<?php echo $timeString; ?>"></span>
            <div class="mtb10">
                <a target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo get_permalink(get_the_ID()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'single-detail', '<?php echo get_permalink(get_the_ID()); ?>']);">
                    <span class="glyphicon glyphicon-comment"></span>
                    <span class="fb-comment-count" data-href="<?php echo get_permalink(get_the_ID()); ?>"></span>
                </a>
                <span class="ml10 fbshare fbshare_0" data-oglink="<?php echo $targetlink; ?>" data-caption="<?php bloginfo('url'); ?>" data-link="<?php echo get_permalink(); ?>" data-picture="<?php echo esc_attr($img); ?>" data-description="<?php echo esc_attr(get_the_excerpt()); ?>" data-name="<?php echo esc_attr(get_the_title()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'facebook-share', '<?php echo get_permalink(get_the_ID()); ?>']);"></span>
                <span class="fbshare_1"></span>
                <span class="fbshare_2"></span>
                <div class="clearfix"></div>
            </div>
            <div class="description-news"><?php the_excerpt(); ?></div>
        </div>
        <?php
    }
    $temp = ob_get_contents();
    ob_end_clean();
    return $temp;
}

/* Begin add Sider bar */

function tinf5Siderbar() {
    register_sidebar(array(
        'name' => __('Home left', 'tinf5'),
        'id' => 'sidebar-home-left',
        'description' => __('Home left block', 'tinf5'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Home right', 'tinf5'),
        'id' => 'sidebar-home-right',
        'description' => __('Home right block', 'tinf5'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}

add_action('widgets_init', 'tinf5Siderbar');
/* End add Sider bar */

/* Begin for cronjob */

/*  Description: Add category ID  to link    */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Array &$rss:  link of rss */
/*      Integer $cate: cagetogy ID */
/*  Return: void   */

function addIDCateToRSS($RSS, $cate) {
    $temp = array();
    if (count($RSS)) {
        foreach ($RSS as $value) {
            $temp[] = str_replace(chr(13), '', $value) . ' ' . $cate;
        }
    }
    return $temp;
}

/*  Description: Load conten from rss then insert theme in to DB    */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      String $rss:  link of rss */
/*  Return: void   */

function loadRSS($rss = '') {
    global $titles;
    $urls = explode(' ', $rss);
    $url = trim($urls[0]);
    $cate = trim($urls[1]);
    $temp = 0;
    $objRss = @simplexml_load_file($url);
    if (isset($objRss->channel->item) && count($objRss->channel->item)) {
        foreach ($objRss->channel->item as $item) {
            if ((date('Y-m-d') == date('Y-m-d', strtotime((string) $item->pubDate))) && !in_array(trim(strip_tags((string) $item->title)), $titles)) {
                $postData = array(
                    'post_title' => trim(wp_strip_all_tags((string) $item->title)),
                    'post_excerpt' => trim(strip_tags((string) $item->description)),
                    'post_date' => date('Y-m-d H:i:s', strtotime((string) $item->pubDate)),
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_category' => array($cate)
                );
                $postID = wp_insert_post($postData);
                if ($postID) {
                    add_post_meta($postID, 'post_link_target', (string) $item->link, TRUE);
                    if (isset($item->enclosure)) {
                        add_post_meta($postID, 'post_img_target', (string) $item->enclosure['url'], TRUE);
                    } elseif (isset($item->summaryImg)) {
                        add_post_meta($postID, 'post_img_target', (string) $item->summaryImg, TRUE);
                    } elseif ((getImageFromDescription((string) $item->description))) {
                        add_post_meta($postID, 'post_img_target', getImageFromDescription($item->description), TRUE);
                    }
                    $titles[] = trim(wp_strip_all_tags((string) $item->title));
                    $temp++;
                }
            }
        }
    } else {
        echo $url;
    }
    return $temp;
}

/*  Description: Get image url from description of RSS   */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      String $description: Content for need url   */
/*  Return: string, fasle   */

function getImageFromDescription($description = '') {
    if (!empty($description)) {
        $pattern = '/<img[^>]*>/i';
        preg_match($pattern, $description, $imgObj);
        if (count($imgObj)) {
            $pattern = '/http[^\'"]*/i';
            preg_match($pattern, $imgObj[0], $imgUrl);
            return $imgUrl[0];
        }
    }
    return FALSE;
}

/*  Description: Get post today   */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      void   */
/*  Return: array of objects   */

function getTitlesToDay() {
    global $titles;
    add_filter('posts_where', 'filter_where');
    $temp = query_posts(array('posts_per_page' => -1, 'post_status' => 'any'));
    if (count($temp))
        foreach ($temp as $item) {
            $titles[] = trim($item->post_title);
        }
    wp_reset_query();
}

/* function for hook condition date */

function filter_where($where = '') {
    $where .= " AND DATE(post_date) = '" . date('Y-m-d') . "'";
    return $where;
}

/*  Description: All post  today */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      void   */
/*  Return: array of objects   */

function allPostToday($cat = 0) {
    add_filter('posts_where', 'filter_where');
    $condition = array('posts_per_page' => -1);
    if ($cat) {
        $condition['cat'] = $cat;
    }
    $temp = query_posts($condition);
    wp_reset_query();
    return $temp;
}

/* End for cronjob */

/*  Description: Get post latest by number and category ID */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Integer $cat   */
/*  Return: array of objects   */

function getPostlatest($num = -1, $cat = 0) {
    add_filter('posts_where', 'filter_where');
    $condition = array(
        'posts_per_page' => $num
    );
    if ($cat) {
        $condition['category'] = array($cat);
    }
    $temp = query_posts($condition);
    wp_reset_query();
    return $temp;
}

/*  Description: All post  today */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      void   */
/*  Return: array of objects   */

function metaTags() {
    $posts = getPostlatest(1);
    if (count($posts)) {
        $post = $posts[0];
        ?>
        <meta property="og:title" content="<?php echo esc_attr($post->post_title); ?>">
        <meta property="og:site_name" content="Scrumptious">
        <meta property="og:description" content="<?php echo esc_attr($post->post_content); ?>">
        <meta property="og:image" content="<?php echo get_post_meta($post->ID, 'post_img_target', TRUE) ?>">
        <?php
    }
}

//add_action('wp_head', 'metaTags');

/*  Description: get URL image  */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Integer  $id */
/*  Return: String URL   */

function getURLImage($id) {
    $img = get_post_meta($id, 'post_img_target', TRUE);
    if (empty($img)) {
        return home_url('wp-content/themes/tinf5/images/icon/noimage.gif');
    }
    if (strpos($img, 'http://') === FALSE) {
        $url = get_post_meta($id, 'post_link_target', TRUE);
        $result = parse_url($url);
        return $result['scheme'] . "://" . $result['host'] . $img;
    }
    return $img;
}

/*  Description: get domain name link extend  */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Integer  $id */
/*  Return: Domain name   */

function getURLExtend($id) {
    $url = get_post_meta($id, 'post_link_target', TRUE);
    $result = explode('/', $url);
    return str_replace('www.', '', $result[2]);
}

/*  Description: get post item type  */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Void */
/*  Return:     */

function getPostItem($type = 0) {
    ob_start();
    $img = getURLImage(get_the_ID());
    $targetlink = get_post_meta(get_the_ID(), 'post_link_target', TRUE);
    $timeString = get_the_date();
    $timeUnix = get_post_time();
    $domain = getURLExtend(get_the_ID());
    switch ($type) {
        case 1:
            ?>
            <div class="col-xs-12 col-sm-12 col-md-4 tc h3block">
                <h1><a target="_blank" href="<?php echo $targetlink; ?>" onclick="javascript:_gaq.push(['_trackEvent', 'extend-link', '<?php echo $targetlink; ?>']);" title="<?php echo esc_attr(get_the_title()); ?>"><img class="img-responsive img-thumbnail mc" src="<?php echo $img; ?>" alt="<?php echo esc_attr(get_the_excerpt()); ?>"></a></h1>
                <div class="h3title"><a target="_blank" href="<?php echo $targetlink; ?>" onclick="javascript:_gaq.push(['_trackEvent', 'extend-link', '<?php echo $targetlink; ?>']);" title="<?php echo esc_attr(get_the_title()); ?>"><?php the_title(); ?></a></div>
                <span class="time"><?php echo $domain; ?></span>
                <span class="time converttimetostring mb10" data-timeUnix="<?php echo $timeUnix ?>" data-string="<?php echo $timeString; ?>"></span>
                <div class="mtb10">
                    <a class="fl" target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo get_permalink(get_the_ID()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'single-detail', '<?php echo get_permalink(get_the_ID()); ?>']);">
                        <span class="glyphicon glyphicon-comment"></span>
                        <span class="fb-comment-count" data-href="<?php echo get_permalink(get_the_ID()); ?>"></span>
                    </a>
                    <span class="ml10 fbshare fbshare_0" data-oglink="<?php echo $targetlink; ?>" data-caption="<?php bloginfo('url'); ?>" data-link="<?php echo get_permalink(); ?>" data-picture="<?php echo esc_attr($img); ?>" data-description="<?php echo esc_attr(get_the_excerpt()); ?>" data-name="<?php echo esc_attr(get_the_title()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'facebook-share', '<?php echo get_permalink(get_the_ID()); ?>']);"></span>
                    <span class="fbshare_1"></span>
                    <span class="fbshare_2"></span>
                    <div class="clearfix"></div>
                </div>
            </div>
            <?php
            break;
        case 2:
            ?>
            <div class="row">
                <h1><a target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo $targetlink; ?>" onclick="javascript:_gaq.push(['_trackEvent', 'extend-link', '<?php echo $targetlink; ?>']);"><?php the_title(); ?></a></h1>
                <span class="time"><?php echo $domain; ?></span>
                <span class="time converttimetostring" data-timeUnix="<?php echo $timeUnix ?>" data-string="<?php echo $timeString; ?>"></span>
                <div class="description-news"><?php echo esc_attr(get_the_excerpt()); ?></div>
                <div class="mtb10">
                    <a class="fl" target="_blank" title="<?php echo esc_attr(get_the_title()); ?>" href="<?php echo get_permalink(get_the_ID()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'single-detail', '<?php echo get_permalink(get_the_ID()); ?>']);">
                        <span class="glyphicon glyphicon-comment"></span>
                        <span class="fb-comment-count" data-href="<?php echo get_permalink(get_the_ID()); ?>"></span>
                    </a>
                    <span class="ml10 fbshare fbshare_0" data-oglink="<?php echo $targetlink; ?>" data-caption="<?php bloginfo('url'); ?>" data-link="<?php echo get_permalink(); ?>" data-picture="<?php echo esc_attr($img); ?>" data-description="<?php echo esc_attr(get_the_excerpt()); ?>" data-name="<?php echo esc_attr(get_the_title()); ?>" onclick="javascript:_gaq.push(['_trackEvent', 'facebook-share', '<?php echo get_permalink(get_the_ID()); ?>']);"></span>
                    <span class="fbshare_1"></span>
                    <span class="fbshare_2"></span>
                    <div class="clearfix"></div>
                </div>
            </div>
            <?php
            break;
    }
    $temp = ob_get_contents();
    ob_end_clean();
    return $temp;
}

/*
  /*  Description: get post item for ajax */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Integer $num: */
/*      Integer $cat: */
/*      Integer $typet: */
/*  Return:     */

function getnewsAjax($num = -1, $cat = 0, $type = 0) {
    add_filter('posts_where', 'filter_where');
    $condition = array(
        'posts_per_page' => $num
    );
    if ($cat) {
        $condition['category'] = array($cat);
    }
    query_posts($condition);
    $temp = '';
    while (have_posts()) : the_post();
        switch ($type) {
            case 0:
                $temp .= getItemPostNews();
                break;
            case 1:
                $temp .= getPostItem($type);
                break;
            case 2:
                $temp .= getPostItem($type);
                break;
        }
    endwhile;
    wp_reset_query();
    return $temp;
}

/*
  /*  Description: get post item for ajax */
/*  Author: MrQuyet */
/*  Paramater:  */
/*      Integer $num: */
/*      Integer $cat: */
/*      Integer $typet: */
/*  Return:     */

function getRegularEx($domain = '') {
    $domainlist = of_get_option('domainlist');
    $domainlist = explode(chr(13), $domainlist);
    foreach ($domainlist as $item) {
        if (strpos($item, $domain) !== FALSE) {
            $temp = explode(' ', $item);
            if (isset($temp[1])) {
                return $temp[1];
            }
        }
    }
    return FALSE;
}
?>